﻿using Dapper.DBContext;
using Guoc.BigMall.Application.DTO;
using Guoc.BigMall.Application.Search;
using Guoc.BigMall.Application.ViewObject;
using Guoc.BigMall.Domain;
using Guoc.BigMall.Domain.Entity;
using Guoc.BigMall.Domain.ValueObject;
using Guoc.BigMall.Infrastructure;
using Guoc.BigMall.Infrastructure.Extension;
using Guoc.BigMall.Infrastructure.Log;
using Guoc.BigMall.Infrastructure.Utils;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq.Expressions;

namespace Guoc.BigMall.Application.Facade
{
    public class SaleOrderFacade : ISaleOrderFacade
    {

        IDBContext _db;
        IContextFacade _context;
        ILogger _log;
      
        public SaleOrderFacade( IDBContext db,IContextFacade context,ILogger log)
        {
            _db = db;
            _context = context;
            _log = log;
        }

        public IEnumerable<SaleOrderListDto> GetList(Pager page, SearchSaleOrder search)
        {
            IEnumerable<SaleOrderListDto> rows;
            dynamic param = new ExpandoObject();
            string where = "";
            if (search.OrderCode.NotNullOrEmpty())
            {
                where += " and  s.Code like @Code";
                param.code = string.Format("%{0}%", search.OrderCode);
            }
            if (search.ReceiverTel.NotNullOrEmpty())
            {
                where += " and  s.ReceiverTel like @ReceiverTel";
                param.ReceiverTel = string.Format("{0}%", search.ReceiverTel);
            }
            if (search.ReceiverName.NotNullOrEmpty())
            {
                where += " and  s.ReceiverName like @ReceiverName";
                param.ReceiverName =string.Format("{0}%", search.ReceiverName);
            }

            if (search.ProductName.NotNullOrEmpty())
            {
                where += " and p.Name like @Name";
                param.name = string.Format("%{0}%", search.ProductName);
            }

            if (search.ProductCode.NotNullOrEmpty())
            {
                where += " and p.Code = @ProductCode";
                param.ProductCode =  search.ProductCode;
            }
            if (search.Status.HasValue)
            {
                where += " and s.Status = @Status";
                param.Status = search.Status.Value;
            }
            if (search.PayStatus.HasValue)
            {
                where += " and s.PayStatus = @PayStatus";
                param.PayStatus = search.PayStatus.Value;
            }

            string sql = @"Select s.*,p.code as ProductCode,p.Name as ProductName,i.RealPrice,i.Quantity,
m.NickName as MemberName,rm.Id as ReferId,rm.NickName as ReferName from SaleOrder s 
inner join SaleOrderItem i on s.Id = i.SaleOrderId 
inner join Product p on p.Id=i.ProductId 
inner join member m on m.Id = s.MemberId
left join member rm on m.Id = s.ReferMemberId 
where 1=1 {0} Order By  Id desc LIMIT {1},{2}";
            sql = string.Format(sql, where, (page.PageIndex - 1) * page.PageSize, page.PageSize);
            rows = _db.DataBase.Query<SaleOrderListDto>(sql, param);
            var sqlcount = string.Format(@" SELECT count(1) FROM SaleOrder s inner join SaleOrderItem i on s.Id = i.SaleOrderId 
left join Product p on p.Id = i.ProductId  WHERE 1=1 {0} ", where);
            page.Total = this._db.DataBase.ExecuteScalar<int>(sqlcount, param);

            return rows;
        }

        public SaleOrderVo GetOrderDetail(int orderId)
        {
            var sql = "select * from saleorder where id=@OrderId";
            var sqlitem = "select * from SaleOrderItem where saleOrderId=@OrderId";
            var order = _db.DataBase.QuerySingle<SaleOrderVo>(sql, new { OrderId = orderId });
            if (order == null) {
                throw new FriendlyException("订单不存在");                
            }
            var rows = _db.DataBase.Query<SaleOrderItemVo>(sqlitem, new { OrderId = orderId });
            order.Items = rows;
            return order;
        }
    }
}